package com.uhuijia.sso.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.uhuijia.common.pojo.UhuijiaResult;
import com.uhuijia.common.utils.JsonUtils;
import com.uhuijia.jedis.JedisClient;
import com.uhuijia.persistence.usersmall.dao.UsersMallDao;
import com.uhuijia.pojo.usersmall.model.UsersMall;
import com.uhuijia.pojo.usersmall.model.UsersMallExample;
import com.uhuijia.pojo.usersmall.model.UsersMallExample.Criteria;
import com.uhuijia.sso.service.UserService;


/**
 * 用户处理Service
 * <p>Title: UserServiceImpl</p>
 * <p>Description: </p>
 * <p>Company: www.itcast.cn</p> 
 * @version 1.0
 */
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UsersMallDao usersMallDao;
	@Autowired
	private JedisClient jedisClient;
	
	@Value("${USER_SESSION}")
	private String USER_SESSION;
	@Value("${SESSION_EXPIRE}")
	private Integer SESSION_EXPIRE;
	
	@Override
	public UhuijiaResult checkData(String data, int type) {
		
		UsersMallExample example = new UsersMallExample();
		Criteria criteria = example.createCriteria();
		//设置查询条件
		//1.判断用户名是否可用
		if (type == 1) {
			//criteria.andUsernameEqualTo(data);
		//2判断手机号是否可以使用
		} else if (type == 2) {
			//criteria.andPhoneEqualTo(data);
		//3判断邮箱是否可以使用
		} else if (type == 3) {
			criteria.andEmailEqualTo(data);
		} else {
			return UhuijiaResult.build(400, "参数中包含非法数据");
		}
		//执行查询
		List<UsersMall> list = usersMallDao.selectByExample(example);
		if (list !=null && list.size() > 0) {
			//查询到数据，返回false
			return UhuijiaResult.ok(false);
		}
		//数据可以使用
		return UhuijiaResult.ok(true);
	}

	@Override
	public UhuijiaResult register(UsersMall user) {
		/*//检查数据的有效性
		if (StringUtils.isBlank(user.getUsername())) {
			return UhuijiaResult.build(400, "用户名不能为空");
		}
		//判断用户名是否重复
		UhuijiaResult UhuijiaResult = checkData(user.getUsername(), 1);
		if (!(boolean) UhuijiaResult.getData()) {
			return UhuijiaResult.build(400, "用户名重复");
		}
		//判断密码是否为空
		if (StringUtils.isBlank(user.getPassword())) {
			return UhuijiaResult.build(400, "密码不能为空");
		}
		if (StringUtils.isNotBlank(user.getPhone())) {
			//是否重复校验
			UhuijiaResult = checkData(user.getPhone(), 2);
			if (!(boolean) UhuijiaResult.getData()) {
				return UhuijiaResult.build(400, "电话号码重复");
			}
		}
		//如果email不为空的话进行是否重复校验
		if (StringUtils.isNotBlank(user.getEmail())) {
			//是否重复校验
			UhuijiaResult = checkData(user.getEmail(), 3);
			if (!(boolean) UhuijiaResult.getData()) {
				return UhuijiaResult.build(400, "email重复");
			}
		}*/
		//补全pojo的属性
		//user.setCreated(new Date());
		//user.setUpdated(new Date());
		//密码要进行md5加密
		//String md5Pass = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
		//user.setPassword(md5Pass);
		//插入数据
		usersMallDao.insert(user);
		//返回注册成功
		return UhuijiaResult.ok();
	}

	@Override
	public UhuijiaResult login(String username, String password) {
		/*//判断用户名和密码是否正确
		UsersMallExample example = new UsersMallExample();
		Criteria criteria = example.createCriteria();
		criteria.andUsernameEqualTo(username);
		List<UsersMall> list = usersMallDao.selectByExample(example);
		if (list == null || list.size() == 0) {
			//返回登录失败
			return UhuijiaResult.build(400, "用户名或密码不正确");
		}
		UsersMall user = list.get(0);
		//密码要进行md5加密然后再校验
		if (!DigestUtils.md5DigestAsHex(password.getBytes())
				.equals(user.getPassword())) {
			//返回登录失败
			return UhuijiaResult.build(400, "用户名或密码不正确");
		}
		//生成token，使用uuid
		String token = UUID.randomUUID().toString();
		//清空密码
		user.setPassword(null);
		//把用户信息保存到redis，key就是token，value就是用户信息
		jedisClient.set(USER_SESSION + ":" + token, JsonUtils.objectToJson(user));
		//设置key的过期时间
		jedisClient.expire(USER_SESSION + ":" + token, SESSION_EXPIRE);
		//返回登录成功，其中要把token返回。
		return UhuijiaResult.ok(token);*/
		return null;
	}

	@Override
	public UhuijiaResult getUserByToken(String token) {
		String json = jedisClient.get(USER_SESSION + ":" + token);
		if (StringUtils.isBlank(json)) {
			return UhuijiaResult.build(400, "用户登录已经过期");
		}
		//重置Session的过期时间
		jedisClient.expire(USER_SESSION + ":" + token, SESSION_EXPIRE);
		//把json转换成User对象
		UsersMall user = JsonUtils.jsonToPojo(json, UsersMall.class);
		return UhuijiaResult.ok(user);
		//return UhuijiaResult.ok(json);
	}

}